/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amse.grigory.dchess.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.amse.grigory.dchess.kernel.*;
import org.amse.grigory.dchess.rules.Rules;

/**
 *
 * @author grigory
 */
public class DChessText extends JPanel implements IModelListener {

    private IModel myModel;
    private JList myList1,  myList2,  myListC;
    private Vector myData1,  myData2,  myDataC;

    public DChessText(IModel model) {
        super();
        myModel = model;
        myListC = new JList();
        myList1 = new JList();
        myList2 = new JList();
        myListC.setFocusable(false);
        myList1.setFocusable(false);
        myList2.setFocusable(false);
        setLayout(new GridLayout(1, 3));
        add(myListC);
        add(myList1);
        add(myList2);
        myDataC = new Vector();
        myData1 = new Vector();
        myData2 = new Vector();
        myListC.setListData(myDataC);
        myList1.setListData(myData1);
        myList2.setListData(myData2);
        myList1.setFixedCellWidth(WIDTH / 3);
        myList2.setFixedCellWidth(WIDTH / 3);
        myListC.setFixedCellWidth(WIDTH / 3);

    }

    public void update() {
        int n = myModel.getMovesCount();
        myDataC = new Vector();
        myData1 = new Vector();
        myData2 = new Vector();
        myListC.setListData(myDataC);
        myList1.setListData(myData1);
        myList2.setListData(myData2);
        myListC.addListSelectionListener(new ListListener());
        myList1.addListSelectionListener(new ListListener());
        myList2.addListSelectionListener(new ListListener());
        for (int i = 0; i < n; i++) {
            if (i % 2 == 0) {
                myDataC.add("White");
            } else {
                myDataC.add("Black");
            }
            Move move = myModel.getMoveCoordinates(i);
            int sx = move.getSX();
            int sy = move.getSY();
            int ex = move.getEX();
            int ey = move.getEY();
            String s, s2;
            char cx = (char) (((int) 'a') + sx - 1);
            s = "" + cx + ":" + sy;
            myData1.add(s);
            cx = (char) (((int) 'a') + ex - 1);
            s = "" + cx + ":" + ey;
            myData2.add(s);
            myListC.setListData(myDataC);
            myList1.setListData(myData1);
            myList2.setListData(myData2);
        }
        setSelection();
    }

    private class ListListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            setSelection();
        }
    }

    private void setSelection() {
        int curr = myModel.getCurrentMoveNumber();
        myListC.setSelectedIndex(curr);
        myList1.setSelectedIndex(curr);
        myList2.setSelectedIndex(curr);
    }
}